//
//  YZXToolbar.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/28.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXToolbar: UIToolbar {
    
    // 按钮的点击方法的传递
    var barButtonItemCallback:((btn:UIBarButtonItem) -> Void)?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpUI()
    }

    private func setUpUI(){
        let itemSettings = [
            ["imageName": "compose_toolbar_picture", "action": "picture:"],
            ["imageName": "compose_trendbutton_background", "action": "trend:"],
            ["imageName": "compose_mentionbutton_background", "action": "mention:"],
            ["imageName": "compose_emoticonbutton_background", "action": "emoticon:"],
            ["imageName": "compose_add_background", "action": "add:"]
        ]
        
        self.items = [UIBarButtonItem]()
        // 循环添加
        for (index,dict) in itemSettings.enumerate(){
            let imageName = dict["imageName"]!
            //获取点击事件
            let action = dict["action"]!
            
            let barButtonItem = UIBarButtonItem(imageName: imageName, target: self, action: Selector(action), tag: index)
            
            
            self.items?.append(barButtonItem)
            
            //弹簧
            let bounce = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
            self.items?.append(bounce)
            
        }
        // 移除最后一个弹簧
        self.items?.removeLast()
    }
    // MARK: - 按钮点击事件
    @objc private func picture(btn:UIBarButtonItem) {
        barButtonItemCallback!(btn: btn)
    }
    
    @objc private func trend(btn:UIBarButtonItem) {
        barButtonItemCallback!(btn: btn)
    }
    
    @objc private func mention(btn:UIBarButtonItem) {
        barButtonItemCallback!(btn: btn)
    }
    
    @objc private func emoticon(btn:UIBarButtonItem) {
       barButtonItemCallback!(btn: btn)
    }
    
    @objc private func add(btn:UIBarButtonItem) {
        barButtonItemCallback!(btn: btn)
    }
   
    
}